package com.example.cymjoe.huaweishopping.adapter;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.cymjoe.huaweishopping.R;
import com.example.cymjoe.huaweishopping.SPXQActivity;
import com.example.cymjoe.huaweishopping.WebVieww;
import com.example.cymjoe.huaweishopping.bean.SYGridBean;
import com.example.cymjoe.huaweishopping.widget.MyGridView;
import com.squareup.picasso.Picasso;

import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by cymjoe on 2016/12/6.
 */
public class SYLVAdapter extends BaseAdapter {
    private Context context;
    private SYGVAdapter adapter;
    private List<SYGridBean.DataBean.NewRegionListBean.RegionContentBean.ProductListBean> data2 = new ArrayList<>();
    private List<SYGridBean.DataBean.NewRegionListBean> data;


    public SYLVAdapter(Context context, List<SYGridBean.DataBean.NewRegionListBean> data, List<SYGridBean.DataBean.NewRegionListBean.RegionContentBean.ProductListBean> data2) {
        this.context = context;
        this.data = data;
        this.data2 = data2;

    }

    @Override
    public int getCount() {
        return data != null ? data.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        x.view().inject(parent);
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_01, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.t1 = (TextView) convertView.findViewById(R.id.ttt);
            viewHolder.gv = (MyGridView) convertView.findViewById(R.id.myGrid);
            viewHolder.imm = (ImageView) convertView.findViewById(R.id.imm);
            convertView.setTag(viewHolder);

        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        final SYGridBean.DataBean.NewRegionListBean bean = data.get(position);

        if (bean != null) {
            viewHolder.t1.setText("—— —— " + bean.getRegionContent().getName() + " —— ——");
            Picasso.with(context).load(bean.getRegionScrollAds().get(0).getAdPicUrl()).placeholder(R.mipmap.ic_launcher).error(R.mipmap.ic_launcher).into(viewHolder.imm);
            viewHolder.imm.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String ss = bean.getRegionScrollAds().get(0).getAdPrdUrl();
                    Log.e("ss", ss);
                    //http://mw.vmall.com/product/
                    String str = new String("http://mw.vmall.com/product/");
                    String h = ss.replace(str, "");
                    String[] split = h.split("\\.");
                    String s = split[0];
                    Log.e("ssss", s);
                    if (s.equals("http://asale")) {
                        Intent intent = new Intent(context, WebVieww.class);
                        intent.putExtra("path", ss);
                        context.startActivity(intent);
                    } else {
                        Intent intent = new Intent(context, SPXQActivity.class);


                        intent.putExtra("prdId", s);


                        context.startActivity(intent);
                    }
                }
            });

            final List<SYGridBean.DataBean.NewRegionListBean.RegionContentBean.ProductListBean> list = bean.getRegionContent().getProductList();
            adapter = new SYGVAdapter(context, list);
            int type = bean.getRegionContent().getType();
            if (type == 7) {
                viewHolder.gv.setNumColumns(3);
            } else {
                viewHolder.gv.setNumColumns(2);
            }
            viewHolder.gv.setAdapter(adapter);
            viewHolder.gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Intent intent = new Intent(context, SPXQActivity.class);
                    String prdId = list.get(position).getPrdId();
                    intent.putExtra("prdId", prdId);

                    context.startActivity(intent);
                }
            });
        }

        return convertView;
    }


    class ViewHolder {
        TextView t1;
        MyGridView gv;
        ImageView imm;
    }
}
